package com.wsoft.bpm.listener;

import com.wsoft.bpm.service.ActivitiCallAble;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 流程结束回调监听器
 *
 * @Author zhonglj
 * @Date 2024-09-23 15:43
 */
@Component("endEventCallBackListener")
public class EndEventCallBackListener implements ExecutionListener {
    @Resource
    private RepositoryService repositoryService;

    @Override
    public void notify(DelegateExecution execution) {
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .processDefinitionId(execution.getProcessDefinitionId())
                .singleResult();
        //回调完成
        ActivitiCallAble callService = ActivitiCallAble.findCallService(processDefinition.getKey());
        if (callService != null) {
            callService.callComplete(processDefinition.getKey(), execution.getProcessInstanceId(), execution.getProcessInstanceBusinessKey());
        }
    }
}
